﻿using System.Text;
using Microsoft.International.Converters.PinYinConverter;

namespace EstateNuke.Data
{
    public static class Extensions
    {
        #region Get PinYin

        /// <summary>
        /// </summary>
        /// <param name = "source"></param>
        /// <returns></returns>
        public static string GetPinYin(this string source)
        {
            var builder = new StringBuilder();
            if (source != null)
            {
                foreach (var character in source)
                {
                    if (ChineseChar.IsValidChar(character))
                    {
                        var chineseChar = new ChineseChar(character);
                        if (chineseChar.PinyinCount > 0)
                        {
                            builder.Append(chineseChar.Pinyins[0]);
                        }
                    }
                    else
                    {
                        builder.Append(character);
                    }
                }
            }
            return builder.ToString();
        }

        /// <summary>
        /// </summary>
        /// <param name = "source"></param>
        /// <returns></returns>
        public static string GetFirstPinYin(this string source)
        {
            var builder = new StringBuilder();
            if (source != null)
            {
                foreach (var character in source)
                {
                    if (ChineseChar.IsValidChar(character))
                    {
                        var chineseChar = new ChineseChar(character);
                        if (chineseChar.PinyinCount > 0)
                        {
                            builder.Append(chineseChar.Pinyins[0][0]);
                        }
                    }
                    else
                    {
                        builder.Append(character);
                    }
                }
            }
            return builder.ToString();
        }

        #endregion
    }
}